package github.liweiwei.sort;

import github.liweiwei.utils.SortUtil;

import java.util.Arrays;

/**
 * @Author: DRevelation
 * @Date: 2021/9/16 15:35
 */
public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 1, 3};
        System.out.println("======排序前arr: " + Arrays.toString(arr));
        selectSort(arr);
        System.out.println("======排序后arr: " + Arrays.toString(arr));
    }

    private static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minPos] > arr[j]) {
                    minPos = j;
                }
            }
            SortUtil.swap(arr, minPos, i);
            System.out.println("第" + i + "排序后: " + Arrays.toString(arr));
        }
    }
}
